package com.shepin.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.shepin.R;
import com.shepin.Util.ShePinUtils;
import com.shepin.adapter.CategoryAdapter;
import com.shepin.bean.CategoryBean;
import com.shepin.pullrefresh.PullToRefreshBase;
import com.shepin.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.shepin.pullrefresh.PullToRefreshListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * category
 */
public class CategoryActivity extends Fragment {
    private TextView common_title;
    private PullToRefreshListView mPullListView;
    private ListView mListView;
    private CategoryAdapter categoryAdapter;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private boolean mIsStart = true;
    private int mCurIndex = 0;
    private static final int mLoadDataCount = 100;
    private List<CategoryBean> datas=new ArrayList<>();



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fenlei_activity, null);
        initView(v);
		return v;
	}
    public void initView(View view){
        common_title=(TextView)view.findViewById(R.id.common_title);
        common_title.setText("分类");
        mPullListView=(PullToRefreshListView)view.findViewById(R.id.category_listview);
        mPullListView.setPullLoadEnabled(false);
        mPullListView.setScrollLoadEnabled(true);

        //初始化category数据
        for (int i=0;i<ShePinUtils.categotynames.length;i++) {
            CategoryBean clothesbean = new CategoryBean(ShePinUtils.categotynames[i],ShePinUtils.categoryClothes.length,ShePinUtils.categorycollection[i],ShePinUtils.categorybitmaps[i]);
            datas.add(clothesbean);
        }

//        mCurIndex = mLoadDataCount;
        categoryAdapter=new CategoryAdapter(CategoryActivity.this.getActivity(),datas);

        mListView = mPullListView.getRefreshableView();
        mListView.setAdapter(categoryAdapter);

        mPullListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉刷新
               // mIsStart = true;
               // new GetDataTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉加载
              //  mIsStart = false;
               // new GetDataTask().execute();
            }
        });
        setLastUpdateTime();

        mPullListView.doPullRefreshing(true, 500);


    }
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
//            return mStrings;
            return  null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            boolean hasMoreData = true;
            if (mIsStart) {
               // mListItems.addFirst("Added after refresh...");
            } else {
//                int start = mCurIndex;
//                int end = mCurIndex + mLoadDataCount;
//                if (end >= mStrings.length) {
//                    end = mStrings.length;
//                    hasMoreData = false;//没有更多的数据了
//                }
//
//                for (int i = start; i < end; ++i) {
//                    mListItems.add(mStrings[i]);
//                }
//
//                mCurIndex = end;
            }

            categoryAdapter.notifyDataSetChanged();
            mPullListView.onPullDownRefreshComplete();//下拉刷新
            mPullListView.onPullUpRefreshComplete();//上拉加载
            mPullListView.setHasMoreData(hasMoreData);
            setLastUpdateTime();

            super.onPostExecute(result);
        }
    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        mPullListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

}

